

package com.maoshi.shop.mp.comment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.mp.comment.dao.WeixinMaterialMapper;
import com.maoshi.shop.mp.comment.model.WeixinMaterial;
import com.maoshi.shop.mp.comment.service.WeixinMaterialService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @author lgh on 2019/05/14.
 */
@Service
public class WeixinMaterialServiceImpl extends ServiceImpl<WeixinMaterialMapper,WeixinMaterial> implements WeixinMaterialService {

    @Autowired
    private WeixinMaterialMapper weixinMaterialMapper;

    @Autowired
    private WxMpService wxMpService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertBatch(List<WeixinMaterial> allMaterial, String type) {
        WeixinMaterial record = new WeixinMaterial();
        record.setMediaType(type);
        weixinMaterialMapper.deleteById(record.getMediaId());
        if (CollectionUtil.isNotEmpty(allMaterial)) {
            weixinMaterialMapper.insertBatch(allMaterial);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void delete(String mediaId) {
        weixinMaterialMapper.deleteById(mediaId);
        try {
            wxMpService.getMaterialService().materialDelete(mediaId);
        } catch (WxErrorException e) {
            throw new MaoshiShopBindException(e.getError().getErrorMsg());
        }
    }

}
